#include <irrlicht.h>
#include <iostream>
#include <stdio.h>
#include </home/marrabld/Programing/Projects/spellparser/include/irrKlang.h>
#include "game.h"
///btBulletDynamicsCommon.h is the main Bullet include file, contains most common include files.
#include "btBulletDynamicsCommon.h"
#include "basicPhysics.h"

using namespace std;
using namespace irr;
using namespace irrklang;
using namespace videoGame;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

gameState gameEngine::loadScreen()
{

	int ScreenResolution_x = 1280;
	int ScreenResolution_y = 760;
	int BitDepth = 32;
	int lastFPS = -1;
	int textScaleX = 2;
	int textScaleY = 2;
	int imageThresh = 100;
	core::position2d<s32> mousePos;
	core::position2d<s32> result2d;
	GAME_RUN = 0;
	gameState returnType = DONE;
	irr::f32 Time;
	int result = 0;

	core::position2d<s32> pendPos = core::position2d<s32>(350, 250);

	device = createDevice(driverType, core::dimension2d<s32>(ScreenResolution_x, ScreenResolution_y), BitDepth, fullscreen,shadows, false, this);

	 // start the sound engine with default parameters
	 engine = createIrrKlangDevice();
	 engine->play2D("./media/IrrlichtTheme.ogg",true);

	if (device == 0)
		return  DONE; // could not create selected driver.

	video::IVideoDriver* driver = device->getVideoDriver();
	scene::ISceneManager* smgr = device->getSceneManager();

	//Instantiate the timer
	CIrrTimer* Timer = new CIrrTimer(device);

	/*****************************************************************/
	/*---------------------GUI Environment---------------------------*/
	/*****************************************************************/	

	gui::IGUIEnvironment* env = device->getGUIEnvironment();
	env->addStaticText(L"-={Dan's Double Pendulum Simulation}=-",
	core::rect<int>(10,10,200,22), true);

	env ->addImage(driver->getTexture("../../media/loadScreen/LoadBackground.png"),core::position2d<s32> (0, 0));
	env ->addImage(driver->getTexture("../../media/loadScreen/Title.png"),core::position2d<s32> (200, 10));
	env ->addImage(driver->getTexture("../../media/loadScreen/pendulum.png"),core::position2d<s32> (350, 250));
	env ->addImage(driver->getTexture("../../media/loadScreen/penText.png"),core::position2d<s32> (200, 400));


	env->addImage(driver->getTexture("../../media/opengllogo.png"),core::position2d<s32> (10, 150) );
	env->addImage(driver->getTexture("../../media/irrlichtlogo2.png"),core::position2d<s32>(10, 50));
	env->addImage(driver->getTexture("../../media/bullet-physics-logo-200.jpg"),core::position2d<s32>(1050, 50));

	// Load something into the background
	// create test cube
        scene::ISceneNode* backgroundNode = smgr->addSphereSceneNode(20);
	backgroundNode->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));
        backgroundNode->setMaterialFlag(video::EMF_LIGHTING, false); // disable dynamic lighting

	// add fly circle animator node
	scene::ISceneNodeAnimator* anim =
		smgr->createFlyCircleAnimator (core::vector3df(0,0,400),190.0f, -0.0003f);
	backgroundNode->addAnimator(anim);
	anim->drop();


	//Camera
     	// add static camera
        scene::ICameraSceneNode* Camera = smgr->addCameraSceneNode();
        Camera->setPosition(core::vector3df(0,0,-300));

	//event reciever
	SEvent event;

/*********************************************************************************************************/
/*                                      Main Drawing Loop                                                */
/*********************************************************************************************************/

	while(device->run() && driver && GAME_RUN == 0)
	if (device->isWindowActive())
	{
	    //Start the timer
	    Timer->Update();
	    Time = Timer->m_TimeFactor;

        	driver->beginScene(true, true, video::SColor(255,200,200,200));

		smgr->drawAll();
		env->drawAll();
		
		// Get position of cursor and see if we are over the picture
		mousePos = device->getCursorControl()->getPosition();
		result2d.X = mousePos.X - pendPos.X;
		result2d.Y = mousePos.Y - pendPos.Y;
	        result = ((sqrt(result2d.X*result2d.X + result2d.Y*result2d.Y)));
			cout<<"result.X is :: "<<result2d.X<<endl;
		 cout<<"result.Y is :: "<<result2d.Y<<endl;

		 cout<<"result is :: "<<result<<endl;

		 // receiver.IsKeyDown(irr::KEY_KEY_S)

  if ((result < imageThresh) && (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN));// && (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN ))
		{
			returnType = DOUBLE_PEND;
			GAME_RUN = 3;
			cout<<"Click"<<endl;
			cout<<result2d.X<<endl;

		}

		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
		  core::stringw str = L"My Crappy physics emulator using -> Irrlicht Engine and Bullet Physics Engine [";
		  str += driver->getName();
		  str += "] FPS:";
		  str += fps;

		  device->setWindowCaption(str.c_str());
		  lastFPS = fps;
		}
    Timer->Reset();
	}

	device->drop();
	engine->drop();
	return  returnType;
};







